package com.clothing.controller.app;

import com.clothing.entity.BlackList;
import com.clothing.service.BlackListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.clothing.bean.UserJson;
import com.clothing.controller.BaseController;
import com.clothing.entity.Friend;
import com.clothing.entity.Friend.FriendStatus;
import com.clothing.entity.Friend.Status;
import com.clothing.entity.User;
import com.clothing.service.FriendService;
import com.clothing.service.UserService;
import com.clothing.utils.AjaxMsg;
import com.clothing.utils.Utils;

import java.util.Date;

/**
 * @ClassName: UserController
 * @Description: TODO(用户)
 * @author Charles.Tao
 * @date 2017年9月5日 下午2:14:23
 * @version 1.0
 */
@RequestMapping("/app/user")
@Controller("appUserController")
public class UserController extends BaseController
{
    @Autowired
    private UserService userService;
    
    @Autowired
    private FriendService friendService;

    @Autowired
    private BlackListService blackListService;

    /**
     * @Title: update 
     * @Description: TODO(修改用户信息) 
     * @param @param headImgUrl
     * @param @param nickname
     * @param @param mobile
     * @param @param address
     * @param @param sex
     * @param @param brief
     * @param @return 设定文件 
     * @return String 返回类型  
     * @throws
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public String update(String headImgUrl, String nickname, String mobile, String address, Integer sex, String brief, String weChat)
    {
        if (Utils.isEmpty(headImgUrl))
        {
            return AjaxMsg.failed("头像不能为空");
        }
        
        if (Utils.isEmpty(nickname))
        {
            return AjaxMsg.failed("昵称不能为空");
        }
        
        if (null == sex || (0 != sex && 1 != sex))
        {
            return getParameterErrorMsg();
        }
        
        if (!Utils.isEmpty(mobile) && !Utils.isMobile(mobile))
        {
            return AjaxMsg.failed("手机号格式不正确");
        }
      
        User user = userService.getCurrent();
        user.setHeadImgUrl(headImgUrl);
        user.setNickname(nickname);
        user.setMobile(mobile);
        user.setSex(sex);
        user.setBrief(brief);
		user.setAddress(address);
		user.setWeChat(weChat);
        userService.update(user);
        
        return AjaxMsg.success("修改成功");
    }
    
    /**
     * @Title: myInfo 
     * @Description: TODO(获取用户信息)
     * @param @param id
     * @param @return 设定文件 
     * @return String 返回类型 
     * @throws
     */
    @RequestMapping(value = "/getUserInfo.json", method = RequestMethod.GET)
    @ResponseBody
    public String userInfo(Long userId)
    {
        User current = userService.getCurrent();
        User user;
        
        if (null == userId)
        {
            user = current;
        }
        else
        {
            user = userService.find(userId);
            
            if (null == user)
            {
                return getParameterErrorMsg();
            }
        }
            
        UserJson json = new UserJson();
        json.setNickname(user.getNickname());
        json.setHeadImgUrl(user.getHeadImgUrl());
        json.setMobile(user.getMobile());
        json.setSex(user.getSex());
        json.setBrief(user.getBrief());        
		json.setAddress(user.getAddress());
		json.setWeChat(user.getFullWeChat());
		
        if (null != userId && !current.getId().equals(userId))
        {
            Friend friend = friendService.findFriend(userId, current.getId());
            
            if (null == friend)
            {
                json.setFriendStatus(FriendStatus.stranger);
            }
            else
            {
                Status status = friend.getStatus();
                
                if (Status.mutual == status)
                {
                    json.setFriendStatus(FriendStatus.friend);
                }
                else
                {
                    boolean isBig = friend.isBig(current);
                    
                    if (isBig)
                    {
                        if (Status.big_to_small == status)
                        {
                            json.setFriendStatus(FriendStatus.attention);
                        }
                        else 
                        {
                            json.setFriendStatus(FriendStatus.fans);
                        }
                    }
                    else
                    {
                        if (Status.big_to_small == status)
                        {
                            json.setFriendStatus(FriendStatus.fans);
                        }
                        else 
                        {
                            json.setFriendStatus(FriendStatus.attention);
                        }
                    }
                }
            }
        }
        
        return new AjaxMsg(true, json).toString();
    }

    /**
     * 用户黑名单状态
     *
     * @return
     */
    @RequestMapping(value = "/getBlackListStatus", method = RequestMethod.GET)
    @ResponseBody
    public String blackListStatus() {
        Long userId = userService.getCurrentId();
        BlackList blackList = blackListService.findOne(userId);
        if (null == blackList) {
            return AjaxMsg.success("normal");
        }
        if (new Date().after(blackList.getInvalidDate())) {
            blackListService.delete(blackList);
        }
        return AjaxMsg.success(blackList.getStatus().toString());
    }

    /**
     * 消息通知
     *
     * @return
     */
    @RequestMapping(value = "/getMessageNotice.json", method = RequestMethod.GET)
    @ResponseBody
    public String messageNotice() {
        User current = userService.getCurrent();
        return AjaxMsg.success(current.getMessageNotice().toString());
    }

    /**
     * 读消息
     *
     * @return
     */
    @RequestMapping(value = "/readMessage", method = RequestMethod.GET)
    @ResponseBody
    public String readMessage() {
        User current = userService.getCurrent();
        current.setMessageNotice(Boolean.FALSE);
        userService.update(current);
        return AjaxMsg.success();
    }
}
